package com.shortvedio.media.service.jobhandler;

import com.shortvedio.media.service.TaskVedioService;
import com.shortvedio.media.service.MediaService;
import com.shortvedio.media.service.VideosContentService;
import com.shortvedio.base.model.po.TaskVedio;
import com.shortvedio.base.model.po.VideosContent;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

@Component
@Slf4j
public class VedioTask {

    @Autowired
    private TaskVedioService taskVedioService;

    @Autowired
    private VideosContentService videosContentService;

    @Autowired
    private MediaService mediaService;

    @XxlJob("vedioTaskHandler")
    public void VedioTaskHandler() throws InterruptedException {//转码成功后将视频信息更新到视频表
        //分片参数
        int shardIndex = XxlJobHelper.getShardIndex();
        int shardTotal = XxlJobHelper.getShardTotal();
        //确定cup核心数
        int processors = Runtime.getRuntime().availableProcessors();
        //查询待处理的任务
        List<TaskVedio> taskVedios = taskVedioService.selectListByIndex(shardTotal, shardIndex,processors);
        int size = taskVedios.size();
        log.info("取到的视频处理任务数:"+size);
        if (size<=0){
            return;
        }
        //创建线程池
        ExecutorService executorService = Executors.newFixedThreadPool(size);
        //使用计数器
        CountDownLatch countDownLatch = new CountDownLatch(size);
        taskVedios.forEach(taskVedio -> {
            //将任务放入线程池中
            executorService.execute(() -> {
                String inputkey = taskVedio.getInputkey();
                String mp4 = inputkey+".mp4";
                String jpg = inputkey + ".png";
                //通过id更新用户数据情况同时也更新状态
                //不用在乎幂等性
                VideosContent videosContent = videosContentService.getVideosContent(inputkey);
                if (videosContent==null){
                    countDownLatch.countDown();
                    return;
                }
                videosContent.setFilePath(mp4);
                videosContent.setCoverSrc(jpg);
                videosContent.setStatus(1); //更新状态  标记为已经审核通过
                Integer update = videosContentService.updateVideosContent(videosContent);
                if (update<=0){
                    return;
                }
                Integer delete = taskVedioService.deleteTaskVedio(taskVedio.getId());
                if (delete<=0){
                    return;
                }
                countDownLatch.countDown();
            });

        });
        //        准备最大限度等待时间
        countDownLatch.await(30, TimeUnit.MINUTES);
    }

    @XxlJob("deletePastDue")
    public void deletePastDue() throws InterruptedException {//删除过去视频
        //分片参数
        int shardIndex = XxlJobHelper.getShardIndex();
        int shardTotal = XxlJobHelper.getShardTotal();
        //确定cup核心数
        int processors = Runtime.getRuntime().availableProcessors();
        //查询待处理的任务
        List<TaskVedio> taskVedios = taskVedioService.getPastDueList(shardTotal, shardIndex,processors);
        int size = taskVedios.size();
        log.debug("取到的视频处理任务数:"+size);
        if (size<=0){
            return;
        }

        //创建线程池
        ExecutorService executorService = Executors.newFixedThreadPool(size);
        //使用计数器
        CountDownLatch countDownLatch = new CountDownLatch(size);
        taskVedios.forEach(taskVedio -> {
            executorService.execute(() -> {
                String inputkey = taskVedio.getInputkey();
                String mp4 = inputkey+".mp4";
                String jpg = inputkey + ".png";
                //删除文件
                mediaService.deleteFile(mp4);
                mediaService.deleteFile(jpg);
                //删除任务表中的信息
                taskVedioService.deleteTaskVedio(taskVedio.getId());
                countDownLatch.countDown();
            });

        });
        countDownLatch.await(30, TimeUnit.MINUTES);
    }



}
